/*
 * FileName：SouhuNewsJob.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.webmagic.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smart.common.util.DateUtil;
import com.smart.common.util.HttpClientUtils;
import com.smart.webmagic.processor.SouhuProcessor;
import com.smart.wx.wxcms.entity.WxcmsNews;
import com.smart.wx.wxcms.service.WxcmsNewsService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import javax.annotation.Resource;
import java.io.Serializable;

@Slf4j
@Component
public class SouhuNewsJob implements Job,Serializable {

    @Resource
    private WxcmsNewsService wxcmsNewsService;

    @Resource
    private SouhuProcessor souhuProcessor;
    /**
     * 获取阅读数
     */
    public static final String pvUrl="http://v2.sohu.com/public-api/articles/%s/pv";

    /**
     * 获取24小时热门新闻
     */
    public static final String hotNews="http://v2.sohu.com/public-api/hotArticle?scene=CHANNEL&sceneId=%s";

    /**
     * 搜狐新闻前缀
     */
    public static final String souhuNewsUrl="http://www.sohu.com/a/%s_%s";

    /**
     * 每天早晨1點更新
     *
     * @author hermit
     * @date 2017 -08-08 18:17:31
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        //旅游
        WxcmsNews wxcmsNews;
        String hotNews = HttpClientUtils.sendHttpGet(String.format(this.hotNews, "29"));
        JSONArray array = JSONArray.parseArray(hotNews);
        for (int i = 0; i < array.size(); i++) {
            wxcmsNews = new WxcmsNews();
            JSONObject jsonObject = array.getJSONObject(i);
            wxcmsNews.setNewsOrigin("搜狐");
            wxcmsNews.setTitle(jsonObject.getString("title"));
            wxcmsNews.setNewsUrl(String.format(this.souhuNewsUrl, jsonObject.getString("id"), jsonObject.getString("authorId")));
            wxcmsNews.setPublishTime(DateUtil.timestampToDateStr(jsonObject.getString("publicTime")));
            wxcmsNews.setNewsTypeUrl("http://travel.sohu.com");
            wxcmsNews.setNewsType("旅游");
            try {
                wxcmsNewsService.insert(wxcmsNews);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        hotNews = HttpClientUtils.sendHttpGet(String.format(this.hotNews, "25"));
        array = JSONArray.parseArray(hotNews);
        for (int i = 0; i < array.size(); i++) {
            wxcmsNews = new WxcmsNews();
            JSONObject jsonObject = array.getJSONObject(i);
            wxcmsNews.setNewsOrigin("搜狐");
            wxcmsNews.setTitle(jsonObject.getString("title"));
            wxcmsNews.setNewsUrl(String.format(this.souhuNewsUrl, jsonObject.getString("id"), jsonObject.getString("authorId")));
            wxcmsNews.setPublishTime(DateUtil.timestampToDateStr(jsonObject.getString("publicTime")));
            wxcmsNews.setNewsTypeUrl("http://learning.sohu.com");
            wxcmsNews.setNewsType("教育");
            try {
                wxcmsNewsService.insert(wxcmsNews);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        hotNews = HttpClientUtils.sendHttpGet(String.format(this.hotNews, "30"));
        array = JSONArray.parseArray(hotNews);
        for (int i = 0; i < array.size(); i++) {
            wxcmsNews = new WxcmsNews();
            JSONObject jsonObject = array.getJSONObject(i);
            wxcmsNews.setNewsOrigin("搜狐");
            wxcmsNews.setTitle(jsonObject.getString("title"));
            wxcmsNews.setNewsUrl(String.format(this.souhuNewsUrl, jsonObject.getString("id"), jsonObject.getString("authorId")));
            wxcmsNews.setPublishTime(DateUtil.timestampToDateStr(jsonObject.getString("publicTime")));
            wxcmsNews.setNewsTypeUrl("http://it.sohu.com");
            wxcmsNews.setNewsType("科技");
            try {
                wxcmsNewsService.insert(wxcmsNews);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        hotNews = HttpClientUtils.sendHttpGet(String.format(this.hotNews, "15"));
        array = JSONArray.parseArray(hotNews);
        for (int i = 0; i < array.size(); i++) {
            wxcmsNews = new WxcmsNews();
            JSONObject jsonObject = array.getJSONObject(i);
            wxcmsNews.setNewsOrigin("搜狐");
            wxcmsNews.setTitle(jsonObject.getString("title"));
            wxcmsNews.setNewsUrl(String.format(this.souhuNewsUrl, jsonObject.getString("id"), jsonObject.getString("authorId")));
            wxcmsNews.setPublishTime(DateUtil.timestampToDateStr(jsonObject.getString("publicTime")));
            wxcmsNews.setNewsTypeUrl("http://business.sohu.com");
            wxcmsNews.setNewsType("财经");
            try {
                wxcmsNewsService.insert(wxcmsNews);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        hotNews = HttpClientUtils.sendHttpGet(String.format(this.hotNews, "12"));
        array = JSONArray.parseArray(hotNews);
        for (int i = 0; i < array.size(); i++) {
            wxcmsNews = new WxcmsNews();
            JSONObject jsonObject = array.getJSONObject(i);
            wxcmsNews.setNewsOrigin("搜狐");
            wxcmsNews.setTitle(jsonObject.getString("title"));
            wxcmsNews.setNewsUrl(String.format(this.souhuNewsUrl, jsonObject.getString("id"), jsonObject.getString("authorId")));
            wxcmsNews.setPublishTime(DateUtil.timestampToDateStr(jsonObject.getString("publicTime")));
            wxcmsNews.setNewsTypeUrl("http://cul.sohu.com");
            wxcmsNews.setNewsType("文化");
            try {
                wxcmsNewsService.insert(wxcmsNews);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        hotNews = HttpClientUtils.sendHttpGet(String.format(this.hotNews, "43"));
        array = JSONArray.parseArray(hotNews);
        for (int i = 0; i < array.size(); i++) {
            wxcmsNews = new WxcmsNews();
            JSONObject jsonObject = array.getJSONObject(i);
            wxcmsNews.setNewsOrigin("搜狐");
            wxcmsNews.setTitle(jsonObject.getString("title"));
            wxcmsNews.setNewsUrl(String.format(this.souhuNewsUrl, jsonObject.getString("id"), jsonObject.getString("authorId")));
            wxcmsNews.setPublishTime(DateUtil.timestampToDateStr(jsonObject.getString("publicTime")));
            wxcmsNews.setNewsTypeUrl("http://society.sohu.com");
            wxcmsNews.setNewsType("社会");
            try {
                wxcmsNewsService.insert(wxcmsNews);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        hotNews = HttpClientUtils.sendHttpGet(String.format(this.hotNews, "45"));
        array = JSONArray.parseArray(hotNews);
        for (int i = 0; i < array.size(); i++) {
            wxcmsNews = new WxcmsNews();
            JSONObject jsonObject = array.getJSONObject(i);
            wxcmsNews.setNewsOrigin("搜狐");
            wxcmsNews.setTitle(jsonObject.getString("title"));
            wxcmsNews.setNewsUrl(String.format(this.souhuNewsUrl, jsonObject.getString("id"), jsonObject.getString("authorId")));
            wxcmsNews.setPublishTime(DateUtil.timestampToDateStr(jsonObject.getString("publicTime")));
            wxcmsNews.setNewsTypeUrl("http://fun.sohu.com");
            wxcmsNews.setNewsType("搞笑");
            try {
                wxcmsNewsService.insert(wxcmsNews);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
